package com.ktgj.customer.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.common.log.annotation.SignatureValidation;
import com.ktgj.customer.domain.dto.CustomerCardOrderDTO;
import com.ktgj.customer.service.ILoginService;
import feign.Param;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.customer.domain.BusinessOrder;
import com.ktgj.customer.service.IBusinessOrderService;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 订单Controller
 * 
 * @author ruoyi
 * @date 2021-07-30
 */
@ControllerAdvice
@RestController
@RequestMapping("/businessOrder")
public class BusinessOrderController extends BaseController
{
    @Autowired
    private IBusinessOrderService businessOrderService;
    @Autowired
    private ILoginService loginService;

    /**
     * 查询订单列表
     */
    //@PreAuthorize(hasPermi = "customer:businessOrder:list")
//    @GetMapping("/list")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody  BusinessOrder businessOrder){
        startPage();
        List<BusinessOrder> list = businessOrderService.selectBusinessOrderList(businessOrder);
        return getDataTable(list);
    }

    /**
     * 导出订单列表
     */
    //@PreAuthorize(hasPermi = "customer:businessOrder:export")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BusinessOrder businessOrder) throws IOException
    {
        List<BusinessOrder> list = businessOrderService.selectBusinessOrderList(businessOrder);
        ExcelUtil<BusinessOrder> util = new ExcelUtil<BusinessOrder>(BusinessOrder.class);
        util.exportExcel(response, list, "订单数据");
    }

    /**
     * 获取订单详细信息
     */
    //@PreAuthorize(hasPermi = "customer:businessOrder:query")
    @PostMapping("/getOrderInfo")
    public AjaxResult getOrderInfo(@RequestBody Map<String, Long> map)
    {
        Long orderId = map.get("orderId");
        if (orderId != null)
        {
            return AjaxResult.success(businessOrderService.selectBusinessOrderById(orderId));
        }
        return AjaxResult.error("会员订单ID不能为空！");
    }

    /**
     * 新增订单
     */
    //@PreAuthorize(hasPermi = "customer:businessOrder:add")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BusinessOrder businessOrder)
    {
        return toAjax(businessOrderService.insertBusinessOrder(businessOrder));
    }

    /**
     * 修改订单
     */
    //@PreAuthorize(hasPermi = "customer:businessOrder:edit")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BusinessOrder businessOrder)
    {
        return toAjax(businessOrderService.updateBusinessOrder(businessOrder));
    }

    /**
     * 删除订单
     */
    //@PreAuthorize(hasPermi = "customer:businessOrder:remove")
    @Log(title = "订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Long[] orderIds)
    {
        return toAjax(businessOrderService.deleteBusinessOrderByIds(orderIds));
    }

    /**
     * 根据订单状态查询
     * */
    //@SignatureValidation
    @PostMapping("/selectOrder")
    public AjaxResult selectOrder(HttpServletRequest request, @RequestBody Map<String, Object> map){
        Long cuatomerId;
        try {
            cuatomerId = loginService.getLoginCustomer(request).getCustomerId();
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        String orderState = map.get("orderState").toString();
        return AjaxResult.success(businessOrderService.selectBusinessOrderByOrderState(cuatomerId, orderState));
    }

    /**
     * 查询我的全部订单
     * */
    //@SignatureValidation
    @PostMapping("/selectAllOrders")
    public AjaxResult selectAllOrders(HttpServletRequest request){
        Long cuatomerId;
        try {
            cuatomerId = loginService.getLoginCustomer(request).getCustomerId();
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(businessOrderService.selectBusinessOrderByCustomerIdAndOrderState(cuatomerId));
    }

    /**
     * 根据订单号查询会员Id(远程)
     * */
    @PostMapping("/selectBusinessOrderByOrdernum")
    public BusinessOrder selectBusinessOrderByOrdernum(@RequestParam("ordernum") String ordernum){
        return businessOrderService.selectBusinessOrderByOrdernum(ordernum);
    }

    /**
     * 会员购卡订单列表
     * @param customerCardOrder
     * @return
     */
    @PostMapping("/customerCardOrderList")
    public TableDataInfo customerCardOrderList(@RequestBody CustomerCardOrderDTO customerCardOrder) {
        startPage();
        return getDataTable(businessOrderService.customerCardOrderList(customerCardOrder));
    }
}
